<?php
/***********************************************************
	Filename: app/www/control/checkout.php
	Note	: 存储订单信息，同时展示订单的一些其他信息
	Version : 3.0
	Author  : qinggan
	Update  : 2010-05-07
***********************************************************/
class checkout_c extends Control
{
	function __construct()
	{
		parent::Control();
		$this->load_model("cart");
		$this->load_model("checkout");
	}

	function checkout_c()
	{
		$this->__construct();
	}

	//存储订单信息
	function index_f()
	{
		$this->db->close_cache();//禁止缓存
		$this->cart_m->sessid($this->session_lib->sessid);
		$rslist = $this->cart_m->get_all();
		$otype = "cart";
		if(!$rslist)
		{
			$otype = "user";
			$array = array();
			$array["proid"] = "0";
			$array["title"] = $this->trans_lib->safe("product_name");
			$array["amount"] = $this->trans_lib->int("product_amount");
			$array["price"] = $this->trans_lib->float("product_price");
			if(!$array["title"])
			{
				error("产品名称不允许为空",$this->url("cart"));
			}
			if($array["amount"]<1)
			{
				error("产品数量不能小于1",$this->url("cart"));
			}
			$rslist[0] = $array;
		}
		if(!$rslist)
		{
			error("产品信息不允许为空！",$this->url("cart"));
		}
		//地址信息
		$address = array();
		$address["uid"] = $_SESSION["user_id"] ? $_SESSION["user_id"] : "0";
		$address["fullname"] = $this->trans_lib->safe("fullname");
		$address["tel"] = $this->trans_lib->safe("tel");
		$address["mobile"] = $this->trans_lib->safe("mobile");
		$address["email"] = $this->trans_lib->safe("email");
		$address["address"] = $this->trans_lib->safe("address");
		$address["zipcode"] = $this->trans_lib->safe("zipcode");
		$address["note"] = $this->trans_lib->safe("note");
		$address["postdate"] = $this->system_time;
		$sn = $this->checkout_m->save($rslist,$address,$otype);
		if(!$sn)
		{
			error("订单存储失败，请检查！",site_url("cart"));
		}
		else
		{
			$rs = $this->checkout_m->get_one_sn($sn);
			//发送订单邮件
			$this->load_lib("email");
			$this->email_lib->order($id);//通知客户订单信息
			$this->email_lib->order_admin($id);//通知管理员有订单信息
			//删除购物车内容
			$this->cart_m->del();
			error("您的订单已经创建成功，订单编号是：".$sn,site_url("checkout,info","sn=".$sn."&pass=".$rs["pass"]));
		}
	}

	function info_f()
	{
		$sn = $this->trans_lib->safe("sn");
		$pass = $this->trans_lib->safe("pass");
		$rs = $this->checkout_m->get_one_sn($sn,$pass);
		if(!$rs)
		{
			error("没有找到符合您要求的订单信息！",$this->url("index"));
		}
		if(!$_SESSION["user_id"])
		{
			if(!$pass || $pass != $rs["pass"])
			{
				error("您没有查看此订单的权限！",$this->url("index"));
			}
		}
		else
		{
			if($_SESSION["user_id"] != $rs["uid"])
			{
				error("您没有查看此订单的权限",$this->url("index"));
			}
		}
		$this->tpl->assign("rs",$rs);
		$rslist = $this->checkout_m->get_products($rs["id"]);
		$this->tpl->assign("rslist",$rslist);
		$this->tpl->assign("sitetitle","订单编号：".$rs["sn"]." - 查看订单 - 订单中心");
		$leader[0] = array("title"=>"订单中心","url"=>$this->url("order,list"));
		$leader[1] = array("title"=>"查看订单（".$rs["sn"]."）");
		$this->tpl->assign("leader",$leader);
		$print = $this->trans_lib->int("print");
		if($rs["pay_status"] && $rs["pay_type"])
		{
			$pay_rs = $this->checkout_m->pay_one($rs["pay_type"]);
			if($pay_rs)
			{
				$this->tpl->assign("pay_rs",$pay_rs);
			}
		}
		else
		{
			$pay_rslist = $this->checkout_m->pay_list();
			$this->tpl->assign("pay_rslist",$pay_rslist);
		}
		if($price)
		{
			$this->tpl->display("order_print.".$this->tpl->ext);
		}
		else
		{
			$this->tpl->display("order_info.".$this->tpl->ext);
		}
	}
}
?>